package com.xasz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xasz.entity.Jury;
import com.xasz.entity.Result;
import com.xasz.service.IResultService;
import com.xasz.service.ex.DeleteException;
import com.xasz.service.impl.JuryServiceImpl;
import com.xasz.service.impl.ResultServiceImpl;
import com.xasz.util.JsonResult;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cai
 * @since 2022-07-28
 */
@AllArgsConstructor
@RestController
@RequestMapping("result")
public class ResultController  extends BaseController{
    private IResultService iResultService;
    ResultServiceImpl resultService;
    private JuryServiceImpl juryService;
    /**
     * 创建空分数的评分记录
     */
    @PostMapping("{id}")
    public  JsonResult<Void>  inputResult(@PathVariable Integer id,HttpSession session){
        resultService.creatResult(id,getUidFromSession(session));
        return new JsonResult<>(OK);
    }

    /**
     * 查询某一个活动评分记录
     */
    @GetMapping
    public  JsonResult<List<Result>>  getResult(Integer activityId,HttpSession session){
        //权限判断
        if (!juryService.getOne(new QueryWrapper<Jury>().eq("activity_id",activityId).eq("jury_id",getUidFromSession(session))).getName()
                .equals(getUsernameFromSession(session))){
            throw new DeleteException("你没有权限查看该活动维度信息");
        }
        QueryWrapper<Result> wrapper=new QueryWrapper<Result>()
                .eq("activity_id",activityId);
        //查询
        return new JsonResult<>(OK,iResultService.list(wrapper));
    }

    /**
     * 用户进行一次评分
     */
    @PutMapping
    public  JsonResult<Void>  updateResult(@RequestBody Result result, HttpSession session){
        resultService.upDataResult(result,getUidFromSession(session),getUidFromSession(session));
        return new JsonResult<>(OK);
    }





    //超级管理员专用删除两种方法
//-----------------------------------------------------------------------------------------------------------------
//    @DeleteMapping("ByJury")
//    public  JsonResult<Void>  DeleteByJury(@RequestBody Jury jury,HttpSession session){
//        resultService.detectResultByJury(jury.getActivityId(),jury.getId(),getUsernameFromSession(session));
//        return new JsonResult<>(OK);
//    }
//
//    @DeleteMapping("ByCandidate")
//    public  JsonResult<Void>  DeleteByCandidate(@RequestBody Candidate candidate,HttpSession session){
//        resultService.detectResultByCandidate(candidate.getActivityId(),candidate.getId(),getUsernameFromSession(session));
//        return new JsonResult<>(OK);
//    }
//-----------------------------------------------------------------------------------------------------------------

    //超级管理员专用查询
    @GetMapping("super")
    public  JsonResult<List<Result>>  getResult(){
        return new JsonResult<>(OK,iResultService.list());
    }

}
